package com.byzk.p2p.admin.money.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.byzk.p2p.admin.base.util.BaseUtil;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.money.dao.FundRecordDao;
import com.byzk.p2p.admin.money.dao.RechargeDao;
import com.byzk.p2p.admin.money.dto.FundRecord;
import com.byzk.p2p.admin.money.dto.OfflineRecharge;
import com.byzk.p2p.admin.money.dto.RechargeRecord;
import com.byzk.p2p.admin.money.util.FundRecordType;
import com.byzk.p2p.admin.money.util.PayMethod;
import com.byzk.p2p.admin.money.util.RechargeStatus;
import com.byzk.p2p.admin.money.vo.OfflineRechargeVo;
import com.byzk.p2p.admin.money.vo.RechargeRecordExportVo;
import com.byzk.p2p.admin.money.vo.RechargeRecordVo;
import com.byzk.p2p.admin.user.dao.UserAccountDao;
import com.byzk.p2p.admin.user.dto.UserAccount;

@Service
public class RechargeService {
	
	@Autowired
	private RechargeDao rechargeDao;

	@Autowired
	private UserAccountDao accountDao;
	
	@Autowired
	private FundRecordDao fundDao;
	
	public PageInfo<RechargeRecordVo> listByMethod(RechargeRecordVo vo){
		int count=rechargeDao.countByMethod(vo);
		Double sum=rechargeDao.sumByMethod(vo);
		List<RechargeRecordVo> rechargeList=rechargeDao.listByMethod(vo);
		PageInfo<RechargeRecordVo> page=new PageInfo<RechargeRecordVo>(count, vo.getIndex() , vo.getLimit());
		page.setPageList(rechargeList);
		page.setSum(sum);
		return page;
	}
	
	
	/**
	 * 线下充值
	 */
	public PageInfo<OfflineRecharge> listOffline(OfflineRechargeVo vo){
		int count=rechargeDao.countByOffline(vo);
		List<OfflineRecharge> rechargeList=rechargeDao.listByOffline(vo);
		PageInfo<OfflineRecharge> page=new PageInfo<OfflineRecharge>(count, vo.getIndex(),vo.getLimit());
		page.setPageList(rechargeList);
		return page;
	}
	
	/**
	 * 新增/更新
	 * @param record
	 * @return
	 */
	public Integer saveOrUpdate(RechargeRecord record){
		if(record.getId()==null){
			return rechargeDao.insert(record);
		}else{
			System.out.println("测试--------------------------------------------");
			return rechargeDao.update(record);
		}
	}
	
	public RechargeRecord selectById(Integer id){
		return rechargeDao.selectById(id);
	}
	
	public OfflineRecharge getById(Integer id){
		return rechargeDao.getById(id);
	}
	
	public Integer updateOffline(OfflineRecharge offline){
		return rechargeDao.updateOffline(offline);
	}
	
	public List<RechargeRecord> getRecordByCode(String code){
		return rechargeDao.getRecordByCode(code);
	}
	
	public List<RechargeRecordVo> exportList(RechargeRecordExportVo vo){
		return rechargeDao.listByExportVo(vo);
	}
	
	public void autoRecharge(Integer userId,Double paymentTotal){
		UserAccount account=accountDao.getByUserId(userId);
		RechargeRecord record = new RechargeRecord();
		if(account.getBalance()<paymentTotal){
			Double temp=paymentTotal-account.getBalance();
			
			record.setCode(BaseUtil.generateCode(BaseUtil.CHONGZHI));
			record.setBalance(temp);
			record.setCreateTime(new Date());
			record.setFee(0.0);
			record.setMoney(temp);
			record.setPayMethod(PayMethod.SHOUDONG);
			record.setStatus(RechargeStatus.SUCCESS);
			record.setUserId(userId);
			record.setTerminal("PC");
			rechargeDao.insert(record);
			
			addFundRecord(userId,temp,0.0,"PC","还款自动充值",FundRecordType.Type.RECHARGE.index,0.0,account.getBalance()+temp);
			
			account.setBalance(account.getBalance()+temp);
			account.setTotalRecharge(account.getTotalRecharge()+temp);
			account.setTotalIncome(account.getTotalIncome()+temp);
			account.setTotalAmount(account.getTotalAmount()+temp);
			accountDao.update(account);
		}
	}
	
	public Integer addFundRecord(Integer uid,Double income,Double expend,String terminal,String remark,Integer type,Double freezen,Double balance){
		FundRecord record=new FundRecord();
		record.setCode(BaseUtil.generateCode(BaseUtil.ZIJINLIUSHUI));
		record.setCreateTime(new Date());
		record.setRemark(remark);
		record.setType(type);
		record.setUserId(uid);
		record.setIncome(income);
		record.setExpend(expend);
		record.setTerminal(terminal);		
		record.setFreezen(0.0);
		record.setBalance(balance);
		return fundDao.insert(record);
	}
	
	public Integer addRecharge(String code,Integer userId,double amount,String remark,String isOffLine){
		try {
			RechargeRecord record=new RechargeRecord();
			record.setBalance(amount);
			record.setCreateTime(new Date());
			record.setFee(0.0);
			record.setMoney(amount);
			record.setPayMethod(remark);
			record.setUserId(userId);
			record.setTerminal("PC");
			record.setCode(code);
			record.setStatus(RechargeStatus.SUCCESS);
			record.setPayTime(new Date());
			record.setIsOffLine(isOffLine);//是否线下充值，1-是
			rechargeDao.insert(record);
			
			UserAccount account=accountDao.getByUserId(userId);
			
			//增加资金流水记录
			addFundRecord(userId,amount,0.0,"PC",remark,FundRecordType.Type.RECHARGE.index,0.0,account.getBalance()+amount);
			
			account.setBalance(account.getBalance()+amount);
			account.setTotalAmount(account.getTotalAmount()+amount);
			account.setTotalIncome(account.getTotalIncome()+amount);
			account.setTotalRecharge(account.getTotalRecharge()+amount);
//			account.setNotUsedMoney(account.getNotUsedMoney()+amount);
			accountDao.update(account);
		
			//发送充值成功短信
			//smsService.sendRechargeSms(user.getPhone(), Double.parseDouble(pTrdAmt),user.getId());
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return 0;
	}

}
